#include <Windows.h>
#include <glut.h>
#include "floatimage.H"
#include <math.h>
#include <string>

using namespace std;

static GLint    ImageWidth=500;
static GLint    ImageHeight=750;

floatimage withMakeUp;
floatimage withoutMakeUp;
floatimage makeUp(1296,1944);

string filename="translucentShallow";

GLuint displayID;

void makeupDivision();

void init()
{
	glClearColor(0,0,0,0);
	gluOrtho2D(0,ImageWidth,0,ImageHeight);

	glGenTextures(1,&displayID);
	glBindTexture(GL_TEXTURE_2D, displayID);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, makeUp.sizex, makeUp.sizey, 0, GL_RGB, GL_FLOAT, makeUp.buf);
}

void display(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(0.0, ImageWidth, 0.0, ImageHeight);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glViewport(0, 0, ImageWidth, ImageHeight);

	glPolygonMode(GL_FRONT,GL_FILL);

	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D,displayID);

	glBegin(GL_QUADS);
	glTexCoord2f(1.0, 1.0); 
	glVertex2f(0.0, 0.0);
	glTexCoord2f(0.0, 1.0); 
	glVertex2f(ImageWidth, 0.0);
	glTexCoord2f(0.0, 0.0); 
	glVertex2f(ImageWidth, ImageHeight);
	glTexCoord2f(1.0, 0.0); 
	glVertex2f(0.0, ImageHeight);
	glEnd();

	glDisable(GL_TEXTURE_2D);

	glutSwapBuffers();
}

void main(int argc, char *argv[])
{
	string inputA="./data/";
	string inputB="./data/";
	string output="./result/";
	string filenameA=filename;
	string filenameB=filename;

	withoutMakeUp.LoadPFM(inputA.append(filenameA.append("3.pfm")).c_str());
	withMakeUp.LoadPFM(inputB.append(filenameB.append("4.pfm")).c_str());

	makeupDivision();

	makeUp.SavePFM(output.append(filename.append("MD.pfm")).c_str());

	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
	glutInitWindowPosition(100, 100);
	glutInitWindowSize(ImageWidth, ImageHeight);
	glutCreateWindow("Rasterization");
	init();
	glutDisplayFunc(display);
	glutMainLoop();

}

void makeupDivision()
{

	for (int x=0;x<makeUp.sizex;x++)
	{
		for (int y=0;y<makeUp.sizey;y++)
		{
			makeUp.point(x,y).r=withMakeUp.point(x,y).r/withoutMakeUp.point(x,y).r;
			makeUp.point(x,y).g=withMakeUp.point(x,y).g/withoutMakeUp.point(x,y).g;
			makeUp.point(x,y).b=withMakeUp.point(x,y).b/withoutMakeUp.point(x,y).b;

			//makeUp.point(x,y).r=pow((float)makeUp.point(x,y).r,(float)6.2);
			//makeUp.point(x,y).g=pow((float)makeUp.point(x,y).g,(float)6.2);
			//makeUp.point(x,y).b=pow((float)makeUp.point(x,y).b,(float)6.2);
		}
	}
}